#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/10/17 10:23
# @Author : 闫峰
# @File    : template.py
import json

import requests
from retrying import retry

from third_services.WalMart.erp_get_token import get_token_by_erp
from utils.times_tool.time_control import count_time


class Templates:

    def get_templates_by_templateId(self, orderSourceType, OrderSourceId, templateId):
        url = f"https://marketplace.walmartapis.com/v3/settings/shipping/templates/{templateId}"
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(orderSourceType, OrderSourceId)
        }
        res = requests.get(url=url, headers=hearders).json()
        print(json.dumps(res, indent=4))
        # print(res)

    @count_time
    @retry(stop_max_attempt_number=3, wait_fixed=1000)
    def get_all_templates(self, orderSourceType, OrderSourceId):
        url = f"""https://marketplace.walmartapis.com/v3/settings/shipping/templates"""
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(orderSourceType, OrderSourceId)
        }
        res = requests.get(url=url, headers=hearders)
        if res.status_code == 200:
            # return res.json()
            return json.dumps(res.json(),indent=4)
        else:
            raise
        # print(json.dumps(res, indent=4))
        # print(res)


if __name__ == '__main__':
    # Templates().get_templates_by_templateId(45,16640,345396212423200769)
    print(Templates().get_all_templates(45, 16640))